package com.extension.company.view.home.search;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvc.BaseActivity;
import com.extension.company.common.widget.BounceBackViewPager;

import java.util.ArrayList;
import java.util.List;

public class SearchResultActivity extends BaseActivity implements
		View.OnClickListener, ViewPager.OnPageChangeListener, TextWatcher {

	private ImageView searchResultBack;
	private TextView searchResultSearch;
	private EditText searchResultEdt;
	private BounceBackViewPager searchResultViewPage;
	private TextView tag1, tag2;


	private List<Fragment> fmList;

	public SearchResultActivity() {
	}

	public static void intent(Activity activity) {
		Intent intent = new Intent(activity, SearchResultActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initView() {
		searchResultBack = getViewById(R.id.search_result_back);
		searchResultSearch = getViewById(R.id.search_result__search);
		searchResultViewPage = getViewById(R.id.search_result_viewpager);
		searchResultEdt = getViewById(R.id.search_result_edt);
		tag1 = getViewById(R.id.search_result_tag1);
		tag2 = getViewById(R.id.search_result_tag2);


	}

	@Override
	protected void initData() {
		SearchResultSalesFragment searchResultSalesFragment = new SearchResultSalesFragment();
		SearchResultBrokerageFragment searchResultBrokerageFragment = new SearchResultBrokerageFragment();

		fmList = new ArrayList<>();
		fmList.add(searchResultSalesFragment);
		fmList.add(searchResultBrokerageFragment);

		SearchResultViewPageAdapter pageAdapter = new SearchResultViewPageAdapter(getSupportFragmentManager());
		searchResultViewPage.setAdapter(pageAdapter);

	}

	@Override
	protected void initListener() {
		searchResultBack.setOnClickListener(this);
		searchResultViewPage.addOnPageChangeListener(this);
		tag1.setOnClickListener(this);
		tag2.setOnClickListener(this);
		searchResultSearch.addTextChangedListener(this);
	}

	@Override
	public int getMainLayout() {
		return R.layout.activity_search_result;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.search_result_back:
				finish();
				break;
			case R.id.search_result__search:
				// 搜索
				break;

			case R.id.search_result_tag1:
				searchResultViewPage.setCurrentItem(0);
				break;
			case R.id.search_result_tag2:
				searchResultViewPage.setCurrentItem(1);
				break;
			default:
				break;
		}
	}

	@Override
	public void onPageScrolled(int i, float v, int i1) {

	}

	@Override
	public void onPageSelected(int i) {
		setPagerStatus(i);

	}

	@Override
	public void onPageScrollStateChanged(int i) {

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {


	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {

	}

	@Override
	public void afterTextChanged(Editable s) {

	}

	private class SearchResultViewPageAdapter extends FragmentPagerAdapter {

		public SearchResultViewPageAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			return fmList.get(i);
		}

		@Override
		public int getCount() {
			return fmList.size();
		}
	}

	private void setPagerStatus(int temp) {
		tag1.setTextColor(getResources().getColor(R.color.tvc23));
		tag2.setTextColor(getResources().getColor(R.color.tvc23));
		if (temp == 0) {
			tag1.setTextColor(getResources().getColor(R.color.topcolor));
		} else if (temp == 1) {
			tag2.setTextColor(getResources().getColor(R.color.topcolor));

		}
	}
}
